<?
/* check that user is logged in */
require 'login_check.php';

/* connect to mysql db */
require_once 'db_connection.php';

$sql_units = "SELECT greenhouseid, name, token FROM  greenhouse WHERE  user_id=?";

/**
 * Get unit list
 *
 * @param int $userid User ID in db
 * @return array unitlist{['greenhouseid','name','token'], ...}
 */
function getunits($userid) {
	global $db, $sql_units;

	try {
		$ps = $db->prepare($sql_units);
		$ps->execute(array($userid));
		$units = $ps->fetchAll(PDO::FETCH_ASSOC);
		return $units;
	} catch(Exception $e) {
		die("Chyba SQL [get units]");
	}

}

/* get unit list */
$unitlist = getunits($_SESSION['user_id']);

/* print units data */
$number = 1;
foreach ($unitlist as $unit) {
	echo("<div class=\"unit_list\">");
	echo("<h2>".$number.". ".$unit['name']."</h2>");
	echo("Klíč: <b>".$unit['token']."</b>");
	echo("<FORM action=\"./includes/unit_update.php\" method=\"POST\">");
	echo("<INPUT TYPE=\"submit\" name=\"rename\" value=\"Přejmenovat\">");
	echo("<INPUT TYPE=\"text\" name=\"unitname\">");
	echo("<INPUT TYPE=\"submit\" name=\"remove\" value=\"Odstranit\">");
	echo("<INPUT TYPE=\"hidden\" name=\"unitid\"value=\"".$unit['greenhouseid']."\">");
	echo("</FORM>");
	echo("</div>");
	$number = $number + 1;
}

?>
